<?php
class Session
{





    private $sessiondata;
    private $useurl = FALSE;
    private $expire;
    private $sessionuser = NULL;




    /**
     * Konstruktor
     * Ueberprueft, wie Session-informationen uebergeben werden und ruft $this->getData() auf.
     */
    public function __construct()
    {
        global $core;
        if (SESSION_MODE != 'cookie')
        {
            @session_set_cookie_params($core->get('session_expire'));
            $session_name = $core->get('session_name');
            if ($session_name)
            {
                session_name($session_name);
            }
            session_start();
            $session_name = session_name();
            if (!$_COOKIE[$session_name])
            {
                @ini_set('session.use_trans_sid','0');
                $this->useurl = TRUE;
            }
        }
        $this->expire = $core->get('session_expire');
        $this->getData();
    }




    /**
     * Loescht alle Session informationen
     */
    public function destroy()
    {
        if (SESSION_MODE == 'cookie')
        {
            $keys = array_keys($this->sessiondata);
            foreach ($keys as $key)
            {
                setcookie(SESSION_PREFIX.$key,'',time()-86400);
            }
        }
        else
        {
            session_destroy();
        }
        unset($this->sessiondata,$this->useurl,$this->expire);
    }
    
    
    
    
    
    public function user($userid)
    {
        $this->sessionuser = new User($userid);
        return $this->sessionuser;
    }
    
    
    
    
    
    public function getUser()
    {
        return $this->sessionuser;
    }





    public function generateURL($file,$args = array(),$fullurl = FALSE)
    {
        global $core;
        $url = '';
        if ($fullurl)
        {
            $url = $core->get('url');
        }
        $url .= $file.'?';
        if ($this->useurl)
        {
            $args[session_name()] = session_id();
        }
        $arg = array();
        foreach ($args as $key => $value)
        {
            $arg[] = urlencode($key).'='.urlencode($value);
        }
        $url .= implode('&',$arg);
        return $url;
    }





    private function getData()
    {
        if (SESSION_MODE == 'cookie')
        {
            $strlen = strlen(SESSION_PREFIX);
            foreach ($_COOKIE as $key => $value)
            {
                if (substr($key,0,$strlen) == SESSION_PREFIX)
                {
                    $this->sessiondata[substr($key,3)] = $value;
                }
            }
        }
        else
        {
            $strlen = strlen(SESSION_PREFIX);
            foreach ($_SESSION as $key => $value)
            {
                if (substr($key,0,$strlen) == SESSION_PREFIX)
                {
                    $this->sessiondata[substr($key,3)] = $value;
                }
            }
        }
    }





    public function get($name)
    {
        return $this->sessiondata[$name];
    }





    public function set($name,$value)
    {
        $this->sessiondata[$name] = $value;
    }





    public function write()
    {
        if (SESSION_MODE == 'cookie')
        {
            foreach ($this->sessiondata as $key => $value)
            {
                setcookie(SESSION_PREFIX.$key,$value,time()+$this->expire);
            }
        }
        else
        {
            foreach ($this->sessiondata as $key => $value)
            {
                $_SESSION[SESSION_PREFIX.$key] = $value;
            }
        }
    }





    public function loggedin(&$user = NULL)
    {
        if (!$user)
        {
            $user = $this->sessionuser;
        }
        if (!$user)
        {
            return FALSE;
        }
        return $user->get('password') == $this->get('password') && $user->get('username') == $this->get('username');
    }




    
    public function lastURL()
    {
        $this->set('lasturl',$this->generateURL(substr(strrchr($_SERVER['PHP_SELF'],'/'),1),$_GET));
    }
}
?>